<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
      body{
        margin: 0;
        padding: 0;
      }
        .text{
            color: red;
            font-size: 30px;
            width: 100%;
            height: 100vh;
            background: rgb(240, 201, 201);
        }
    </style>
</head>
<body>
   <div class="text"> 
    <div>我是测试页面</div>
</div>
</body>
<script>
    
  // 初始检测
  checkOrientation();
  
  // 监听方向变化
  window.addEventListener("orientationchange", checkOrientation);
  window.addEventListener("resize", checkOrientation); // 兼容某些设备
  
  function checkOrientation() {
    const orientation = window.matchMedia("(orientation: portrait)").matches? "竖屏" : "横屏";
    
    console.log(`当前方向: ${orientation}`);
    document.querySelector('.text div').textContent = `当前是${orientation}模式`;

    
  }
</script>
</html>